package com.erhuogo.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.erhuogo.dao.OneCategoryDao;
import com.erhuogo.entity.OneCategory;
import com.erhuogo.util.DateUtils;

/**
 * 一级分类Dao的实现类
 * @author aibinxiao
 * @date 2016年10月26日 上午11:46:28
 */
public class OneCategoryDaoImpl extends HibernateDaoSupport implements OneCategoryDao{
	// 日志
	private static Logger logger = LoggerFactory.getLogger(OneCategoryDaoImpl.class);

	// 1.获取所有启用的商品一级分类信息集合Dao实现类
	@Override
	public List<OneCategory> findAllOneCategory() {
		String hql = "from OneCategory where status = ?";
		List<OneCategory> result = null;
		List<OneCategory> list = this.getHibernateTemplate().find(hql, 0);
		if(list != null && list.size() > 0){
			logger.info("所有商品一级分类信息集合：" + list);
			result = list;
		}
		return result;
	}

	// 2.分页条件查询符合条件的记录条数
	@Override
	public int countByParams(String id, String name, String code, String startDate, String endDate) {
		int count = 0;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(OneCategory.class);
		if(!StringUtils.isEmpty(id)){
			Integer oneCategoryId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(oneCategoryId));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(code)){
			criteria.add(Restrictions.like("code", code, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("createTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<OneCategory> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list != null && list.size() > 0) {
			count = list.size();
			logger.info("找到("+ list.size() +")个符合查询条件的一级商品分类");
		} else{
			logger.info("没有找到符合查询条件求的一级商品分类");
		}
		return count;
	}

	// 3.分页查询指定页数的所有记录
	@Override
	public List<OneCategory> findPageByParams(String id, String name, String code, String startDate, String endDate,
			int startIndex, int records) {
		List<OneCategory> result = null;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(OneCategory.class);
		if(!StringUtils.isEmpty(id)){
			Integer oneCategoryId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(oneCategoryId));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(code)){
			criteria.add(Restrictions.like("code", code, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("createTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<OneCategory> list = this.getHibernateTemplate().findByCriteria(criteria, startIndex, records);
		if (list != null && list.size() > 0) {
			result = list;
			logger.info("找到("+ list.size() +")个符合查询条件的一级商品分类");
		} else{
			logger.info("没有找到符合查询条件求的一级商品分类");
		}
		return result;
	}

	// 4.根据ID查询一级分类Dao实现类
	@Override
	public OneCategory findById(Integer id) {
		String hql = "from OneCategory where id = ?";
		OneCategory result = null;
		List<OneCategory> list = this.getHibernateTemplate().find(hql, id);
		if(list != null && list.size() > 0){
			logger.info("根据ID:"+id+",查询到一级分类:" + list.get(0));
			result = list.get(0);
		}
		return result;
	}

	// 5.保存或者更新一级分类信息Dao实现类
	@Override
	public void saveOneCategory(OneCategory oneCategory) throws Exception {
		this.getHibernateTemplate().saveOrUpdate(oneCategory);
	}

	// 6.更新三级分类信息状态Dao实现类
	@Override
	public void updateStauts(Integer id, Integer statusCode) throws Exception {
		String hql = "from OneCategory where id = ?";
		List<OneCategory> list = this.getHibernateTemplate().find(hql, id);
		if(list!=null && list.size()>0){
			OneCategory oneCategoty = list.get(0);
			oneCategoty.setStatus(statusCode);
			oneCategoty.setAlterTime(DateUtils.DateToStr(new Date()));
			this.getHibernateTemplate().saveOrUpdate(oneCategoty);
		}
	}
}
